/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;

import com.anwen.mongo.service.impl.ServiceImpl;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.isfd.constant.enums.IsfdMetadataTableNameEnum;
import com.comac.ins.isfd.domain.IsfdAnalysisParameterConfig;
import com.comac.ins.isfd.domain.bo.IsfdAnalysisParameterConfigBo;
import com.comac.ins.isfd.domain.vo.IsfdAnalysisParameterConfigVo;
import com.comac.ins.isfd.service.IIsfdAnalysisParameterConfigService;
import com.comac.ins.isfd.service.IIsfdMetadataImportService;
import com.comac.ins.isfd.service.IIsfdParameterConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 算法层级别的配置参数service层实现类
 *
 * @author hxloongs
 * @date 2024-7-9
 */
@Slf4j
@Service
public class IsfdAnalysisParameterConfigServiceImpl extends ServiceImpl<IsfdAnalysisParameterConfig> implements IIsfdAnalysisParameterConfigService, IIsfdMetadataImportService {
    @Lazy
    @Autowired
    private IIsfdParameterConfigService iIsfdParameterConfigService;

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.ANALYSIS_PARAMETER_CONFIG.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;

        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdAnalysisParameterConfigVo> targetVos = ExcelUtil.importExcel(is, IsfdAnalysisParameterConfigVo.class);
            for (IsfdAnalysisParameterConfigVo vo : targetVos) {
                IsfdAnalysisParameterConfigBo bo = MapstructUtils.convert(vo, IsfdAnalysisParameterConfigBo.class);
                try {
                    // 设置元数据管理id
                    bo.setMetadataManagementId(metadataManagementId);
                    iIsfdParameterConfigService.parameterConfigAnalysisAdd(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("参数库算法参数数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }

}
